﻿//*****************************************************************************
//** Project:PatternWell.PAC
//** Copyright 2011 PatternWell LLC
//** Revision History:
//** Version 1.0 - Initial Release
//*****************************************************************************

using System.ComponentModel;
using System.Runtime.Serialization;
namespace PatternWell.PAC
{
    /// <summary>
    /// Base class that the INotifyProperty changed event, and supports the IDisposable pattern.
    /// </summary>
    [DataContract(Namespace = GlobalInfo.PacNameSpace)]
    public abstract class NotificationBase:DisposalBase, INotifyPropertyChanged
    {
        #region Dispose Implementation - Implementation of the disposal helper method (Do not modify)

        /// <summary>
        /// Helper method that releases resources for this class implementation.
        /// </summary>
        /// <param name="disposed">flag that determines if the class has already been disposed.</param>
        protected override void Dispose(bool disposed)
        {
            ReleaseResourcesForNotificationBase();
            base.Dispose(disposed);
        }

        #endregion

        #region Release Management - Logic used to release resources for this class (Add release logic in the supplied helper methods)

        /// <summary>
        /// Helper method that releases resource for the 'NotificationBase' class.
        /// </summary>
        private void ReleaseResourcesForNotificationBase()
        {
            //Releasing the event PropertyChanged
            _propertyChanged = null;
        }

        #endregion

        #region Event - PropertyChanged

        /// <summary>
        /// Field that backs the event 'PropertyChanged'
        /// </summary>
        private PropertyChangedEventHandler _propertyChanged;

        /// <summary>
        /// Event that is raised when a property on this class has changed.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged
        {
            add { _propertyChanged += value; }
            remove { _propertyChanged -= value; }
        }

        /// <summary>
        /// Helper Method that raises the property changed event for a specified property
        /// </summary>
        /// <param name="sender">Source object that the property has changed on</param>
        /// <param name="e">Property Changed args to be raised.</param>
        protected virtual void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            var handler = _propertyChanged;

            if (handler != null)
            {
                handler.Invoke(sender, e);
            }

        }

        #endregion

    }
}
